﻿using shuatibao.Tool;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace shuatibao.Web.Account
{
    public class AccountAction
    {
        #region 获取用户
        #region 根据unionId 获取用户信息
        public static WechatUserModel getUserInfoWithUnionId(string unionId)
        {
            string strselect = "select top 1 * from Account where union_id = '" + unionId + "'";

            WechatUserModel userModel = getUserInfoManagerWithSQL(strselect);
            return userModel;
        }
        #endregion

        #region 根据公众号iopenid 获取用户信息
        public static WechatUserModel getUserInfoWithGZHOpenId(string openId)
        {
            string strselect = "select top 1 * from Account where gzh_open_id = '" + openId + "'";

            WechatUserModel userModel = getUserInfoManagerWithSQL(strselect);
            return userModel;
        }
        #endregion

        #region 根据小程序openid获取用户信息
        public static WechatUserModel getUserInfoWithXCXOpenId(string openId)
        {
            string strselect = "select top 1 * from Account where xcx_open_id = '" + openId + "'";

            WechatUserModel userModel = getUserInfoManagerWithSQL(strselect);
            return userModel;
        }
        #endregion
        #endregion

        #region 创建用户
      
        #region 根据公众号openid创建用户
        public static void createUserManagerWithGZH(string unionId, string openId)
        {
            createUserManagerWithSQL(unionId, openId, "");
        }
        #endregion

        #region 根据小程序openid创建用户
        public static void createUserManagerWithXCX(string unionId, string openId)
        {
            createUserManagerWithSQL(unionId, "", openId);
        }
        #endregion

        #region 根据SQL创建用户

        public static void createUserManagerWithSQL(string unionId, string gzhOpenid, string xcxOpenid)
        {
            // 连接数据库
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            sqlcon.Open();
            string StrInsert = "";

            StrInsert = "insert into Account (union_id,gzh_open_id,xcx_open_id,avatar,nick_name,createtime,lasttime,external_userid) values (@union_id,@gzh_open_id,@xcx_open_id,@avatar,@nick_name,@createtime,@lasttime,@external_userid)";
            SqlCommand cmd = new SqlCommand(StrInsert, sqlcon);
            // 添加参数并且设置参数值


            // 1. unionId
            cmd.Parameters.Add("@union_id", SqlDbType.VarChar, 100);
            cmd.Parameters["@union_id"].Value = unionId != "" ? unionId : "";

            cmd.Parameters.Add("@gzh_open_id", SqlDbType.VarChar, 100);
            cmd.Parameters["@gzh_open_id"].Value = gzhOpenid != "" ? gzhOpenid : "";

            cmd.Parameters.Add("@xcx_open_id", SqlDbType.VarChar, 100);
            cmd.Parameters["@xcx_open_id"].Value = xcxOpenid != "" ? xcxOpenid : "";


            cmd.Parameters.Add("@avatar", SqlDbType.VarChar, 100);
            cmd.Parameters["@avatar"].Value = "";

            cmd.Parameters.Add("@nick_name", SqlDbType.VarChar, 50);
            cmd.Parameters["@nick_name"].Value = "";

            cmd.Parameters.Add("@createtime", SqlDbType.VarChar, 50);
            cmd.Parameters["@createtime"].Value = DateTime.Now.ToString();

            cmd.Parameters.Add("@lasttime", SqlDbType.VarChar, 50);
            cmd.Parameters["@lasttime"].Value = DateTime.Now.ToString();

            cmd.Parameters.Add("@external_userid", SqlDbType.VarChar, 50);
            cmd.Parameters["@external_userid"].Value = "";


            // 执行插入数据的操作
            cmd.ExecuteNonQuery();
            sqlcon.Close();
        }

        #endregion

        #region 创建用户
        public static WechatUserModel createUserManagerMain(string unionId, string xcxOpenId, string gzhOpenId) {
            // 1. 获取用户信息
            WechatUserModel userModel = getUserInfoWithUnionId(unionId);

            if (userModel.id == null)
            {  // 创建
                if (xcxOpenId != null && xcxOpenId != "")
                {
                    createUserManagerWithXCX(unionId, xcxOpenId);
                }
                else if (gzhOpenId != null && gzhOpenId != "")
                {
                    createUserManagerWithGZH(unionId, gzhOpenId);
                }
            }
            else {         // 修改
                if (xcxOpenId != null && xcxOpenId != "")
                {
                    updateUserInfoManager(unionId, "",xcxOpenId);
                }
                else if (gzhOpenId != null && gzhOpenId != "")
                {
                    updateUserInfoManager(unionId, gzhOpenId,"");
                }
            }

            WechatUserModel newUserModel = getUserInfoWithUnionId(unionId);

            return newUserModel;

        }
        #endregion



        #endregion

        #region 修改
        public static void updateUserInfoManager(string unionId, string gzhOpenId, string xcxOpenId) {
            string strSqls = "";
            if (gzhOpenId != null && gzhOpenId != "")
            {
                strSqls = "update Account set gzh_open_id = '" + gzhOpenId + "' where union_id = '" + unionId + "'";
            }
            else {
                strSqls = "update Account set xcx_open_id = '" + xcxOpenId + "' where union_id = '" + unionId + "'";
            }

            SQLTool.sqlUpdateInfo(strSqls);
        }
        #endregion

        #region 根据SQL获取用户信息
        public static WechatUserModel getUserInfoManagerWithSQL(string sql) {

            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            sqlcon.Open();

            SqlCommand sqlcmd = new SqlCommand(sql, sqlcon);
            SqlDataReader sqlDataReader = sqlcmd.ExecuteReader();


            WechatUserModel wechatUserModel = new WechatUserModel();
            if (sqlDataReader.Read())
            {
                wechatUserModel.id = sqlDataReader["id"].ToString();
                wechatUserModel.open_id_xcx = sqlDataReader["xcx_open_id"].ToString();
                wechatUserModel.open_id_gzh = sqlDataReader["gzh_open_id"].ToString();
                wechatUserModel.union_id = sqlDataReader["union_id"].ToString();
                wechatUserModel.avatar = sqlDataReader["avatar"].ToString();

                wechatUserModel.nick_name = sqlDataReader["nick_name"].ToString();
                wechatUserModel.createtime = sqlDataReader["createtime"].ToString();
                wechatUserModel.lasttime = sqlDataReader["lasttime"].ToString();
                wechatUserModel.external_userid = sqlDataReader["external_userid"].ToString();

            }
            sqlcon.Close();
            return wechatUserModel;



            return wechatUserModel;

        }

        #endregion

        




    }
}